﻿using System;
using System.Collections.Generic;
using System.Text;
using Mighty.Common;
using System.Collections;

namespace Mighty.Brain
{
    public class YoungBrain : BasicBrain
    {
        /// <summary>
        /// 자신의 베팅차례에서 제일 많은 무늬를 기루다로 선택한다
        /// </summary>
        public override Betting BettingTicEvent()
        {
            Betting betting = new Betting();
            betting.isPass = true;

            // 각 무늬 10이상 카드의 개수를 센다
            int heart = 0;
            int clover = 0;
            int spade = 0;
            int diamond = 0;
            for (int i = 0; i < 10; i++)
            {
                HandedCard card = gameInfo.myCards[i];
                if (card.number > 10)
                {
                    if (card.shape == Shape.Clover) clover++;
                    else if (card.shape == Shape.Heart) heart++;
                    else if (card.shape == Shape.Spade) spade++;
                    else if (card.shape == Shape.Diamond) diamond++;
                }
                if (card.shape == Shape.Joker)
                {
                    clover++;
                    heart++;
                    spade++;
                    diamond++;
                }
            }

            // 가장 많은 무늬를 찾아 낸다
            Giruda selectedShape = Giruda.Heart;
            int selectedCount = heart;

            if (clover > selectedCount)
            {
                selectedShape = Giruda.Clover;
                selectedCount = clover;
            }
            if (spade > selectedCount)
            {
                selectedShape = Giruda.Spade;
                selectedCount = spade;
            }
            if (diamond > selectedCount)
            {
                selectedShape = Giruda.Diamond;
                selectedCount = diamond;
            }
            betting.giruda = selectedShape;


            //가장많은 무늬의 카드숫자가 4이상때 배팅을한다(16~18)
            if (selectedCount > 3)
            {
                if (gameInfo.betting.limit > 0)
                {
                    Betting previousBetting = (Betting)gameInfo.bettingHistory[gameInfo.bettingHistory.Count - 1];
                    if (previousBetting.limit < 18)
                    {
                        betting.isPass = false;
                        betting.limit = previousBetting.limit + 1;
                    }
                }
                else
                {
                    betting.isPass = false;
                    betting.limit = 15;
                }
            }
            if (selectedCount > 2)
            {
                if (gameInfo.betting.limit > 0)
                {
                    Betting previousBetting = (Betting)gameInfo.bettingHistory[gameInfo.bettingHistory.Count - 1];
                    if (previousBetting.limit < 16)
                    {
                        betting.isPass = false;
                        betting.limit = previousBetting.limit + 1;
                    }
                }
                else
                {
                    betting.isPass = false;
                    betting.limit = 14;
                }
            }
            return betting;
        }

        public override HandedCard RoundTicEvent()
        {
            HandedCard selectedCard = null;
            HandedCard girudaCard = null;

            int posGirudaCard = -1;
              
            Shape shapeGirudacard = (Shape)gameInfo.betting.giruda;

            int maxValue = 0;
            int maxPosition = 0;

            int minValue = 100;
            int minPosition = 0;

            for (int i=0 ; i<10 ; i++)
            {
                HandedCard card = gameInfo.myCards[i];
                if (card.isPossible)
                {
                    if (card.number > maxValue)
                    {
                        maxValue = card.number;
                        maxPosition = i;
                    }
                }
            }

            for (int i = 0; i < 10; i++)
            {
                HandedCard card = gameInfo.myCards[i];
                if (card.isPossible)
                {
                    if ( card.number < minValue )
                    {
                        minValue = card.number;
                        minPosition = i;
                    }
                }

            }

            for (int i = 0; i < 10; i++)
            {
                HandedCard card = gameInfo.myCards[i];

                if (card.isPossible)
                {
                    if (card.shape == shapeGirudacard)
                    {
                        posGirudaCard = i;
                    }
                    else
                    {
                        posGirudaCard = minPosition;
                    }
                }
            }
                
            ArrayList possibleCards = new ArrayList();
            foreach (HandedCard card in gameInfo.myCards)
            {
                if (card.isPossible) possibleCards.Add(card);
            }

            int posChoice = 0;
            if (gameInfo.attackPlayer.position == gameInfo.myInfo.position)
            {
                posChoice = maxPosition;
            }
            else if (gameInfo.friend.position == gameInfo.myInfo.position)
            {
                posChoice = posGirudaCard;
            }
            else
            {
                posChoice = minPosition;
            }

            selectedCard = gameInfo.myCards[posChoice];

            return selectedCard;
        }
        public override FriendPlayer ChoiceFriendEvent()
        {
            gameInfo.friend.howToSelect = FriendPlayer.HowToSelect.card;
            gameInfo.friend.howToSelect = FriendPlayer.HowToSelect.player;
            HandedCard card = null;
            bool mighty=true;
            bool joker = true;
            int i = 0;

            card = gameInfo.myCards[i];
            for (i = 0; i < 10; i++)
            {
            
                if (card == gameInfo.mighty)
                {
                    mighty = false;
                }
                else if(card.shape == Shape.Joker)
                {
                    joker = false;
                }
            }
            if (mighty)
            {
                gameInfo.friend.cardCondition = gameInfo.mighty;
            }
            else if (joker)
            {
                gameInfo.friend.cardCondition = new Card(Shape.Joker, 0);
            }
            
            return gameInfo.friend;
        }
    }
}

